var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

var ContentSchema = new mongoose.Schema({
	title:String,	//标题
	content:String,	//内容(富文本)
	isdel:Boolean,	//是否被删除
	sort:Number,	//排序
	contenttype: {
		type:ObjectId,
		ref:'ContentType'
	},
	meta:{
		createAt:{
			type:Date,
			default:Date.now()
		},
		updateAt:{
			type:Date,
			default:Date.now()
		}
	}
});

ContentSchema.pre('save',function(next){
	if(this.isNew){
		this.meta.createAt = this.meta.updateAt=Date.now();
		this.isdel = false;
	}else{
		this.meta.updateAt = Date.now();
	}
	next();
});

ContentSchema.statics = {
	fetch:function(cb){
		return this
			.find({isdel:false})
			.exec(cb);
	},
	findById:function(id,cb){
		return this
			.findOne({_id:id})
			.exec(cb);
	},
	delete:function(id,cb){
		console.log(id+"Delete");
		return this
			.findOne({_id:id})
			.remove();
	}
}

module.exports = ContentSchema;